package com.minimalist.lindi.mapper;

import com.minimalist.common.entity.lindi.po.LindiNews;

import com.minimalist.common.entity.lindi.vo.news.LindiNewsQueryVO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 林地指标交易平台_新闻表 映射层。
 *
 * @author 11987
 * @since 2025-04-02
 */
public interface LindiNewsMapper extends BaseMapper<LindiNews>{

    /**
     * 根据新闻ID查询新闻
     *
     * @param newsId 新闻ID
     * @return 新闻
     */
    default LindiNews selectNewsByNewsId(Long newsId){
        return selectOneByQuery(QueryWrapper.create()
                .eq(LindiNews::getNewsId, newsId));
    }

    /**
     * 根据新闻ID删除新闻
     *
     * @param newsId 新闻ID
     */
    default void deleteNewsByNewsId(Long newsId){
        deleteByQuery(QueryWrapper.create().eq(LindiNews::getNewsId, newsId));
    }

    /**
     * 根据新闻ID更新新闻
     *
     * @param newNews 新闻
     */
    default void updateNewsByNewsId(LindiNews newNews){
        updateByQuery(newNews, QueryWrapper.create()
                .eq(LindiNews::getNewsId, newNews.getNewsId()));
    }

    /**
     * 分页查询新闻列表
     *
     * @param queryVO 新闻查询条件
     * @return 新闻分页列表
     */
    default Page<LindiNews> selectPageNewsList(LindiNewsQueryVO queryVO){
        return paginate(queryVO.getPageNum(), queryVO.getPageSize(),
                QueryWrapper.create()
                        .select(LindiNews::getId, LindiNews::getNewsId, LindiNews::getNewsTitle,
                                LindiNews::getNewsTop, LindiNews::getNewsSort, LindiNews::getNewsPicFileId,
                                LindiNews::getPublishDeptId, LindiNews::getPublishAuthorId,
                                LindiNews::getPublishTime, LindiNews::getStatus)
                        .eq(LindiNews::getStatus, queryVO.getStatus())
                        .like(LindiNews::getNewsTitle, queryVO.getNewsTitle())
                        .orderBy(LindiNews::getNewsTop, false)
                        .orderBy(LindiNews::getNewsSort, true));
    }

    /**
     * 查询轮播图新闻列表（置顶且状态正常）
     *
     * @param queryVO 查询条件
     * @return 轮播图新闻列表
     */
    default Page<LindiNews> selectCarouselNewsList(LindiNewsQueryVO queryVO){
        return paginate(queryVO.getPageNum(), queryVO.getPageSize(),
                QueryWrapper.create()
                        .select(LindiNews::getId, LindiNews::getNewsId, LindiNews::getNewsTitle,
                                LindiNews::getNewsTop, LindiNews::getNewsSort, LindiNews::getNewsPicFileId,
                                LindiNews::getPublishTime)
                        .eq(LindiNews::getStatus, queryVO.getStatus())
                        .eq(LindiNews::getNewsTop, true)
                        .isNotNull(LindiNews::getNewsPicFileId)
                        .orderBy(LindiNews::getNewsSort, true)
                        .orderBy(LindiNews::getPublishTime, false));
    }

    /**
     * 查询网站新闻列表（前台展示）
     *
     * @param queryVO 查询条件
     * @return 网站新闻列表
     */
    default Page<LindiNews> selectWebsiteNewsList(LindiNewsQueryVO queryVO){
        return paginate(queryVO.getPageNum(), queryVO.getPageSize(),
                QueryWrapper.create()
                        .select(LindiNews::getId, LindiNews::getNewsId, LindiNews::getNewsTitle,
                                LindiNews::getNewsTop, LindiNews::getNewsSort, LindiNews::getNewsPicFileId,
                                LindiNews::getPublishTime)
                        .eq(LindiNews::getStatus, queryVO.getStatus())
                        .like(LindiNews::getNewsTitle, queryVO.getNewsTitle())
                        .orderBy(LindiNews::getNewsTop, false)
                        .orderBy(LindiNews::getPublishTime, false));
    }

    /**
     * 查询新闻详情（前台展示）
     *
     * @param newsId 新闻ID
     * @return 新闻详情
     */
    default LindiNews selectWebsiteNewsDetail(Long newsId){
        return selectOneByQuery(QueryWrapper.create()
                .eq(LindiNews::getNewsId, newsId)
                .eq(LindiNews::getStatus, 1));
    }
}
